﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Lyon.Utility
{
    /// <summary>
    /// 超级定时器
    /// </summary>
    public class SuperTimer : IDisposable
    {
        Timer timer;
        ITimeSet value;
        Callback callback;
        public SuperTimer(ITimeSet value, Callback callback)
        {
            this.value = value;
            this.callback = callback;
            timer = new Timer(TimerCallback, this, 1000, 1000);
        }

        void TimerCallback(object state)
        {
            DateTime temp = DateTime.Now;
            if (value.Contains(temp))
                callback();
        }

        public void Dispose()
        {
            timer.Dispose();
        }
    }
    /// <summary>
    /// 时间集
    /// </summary>
    public interface ITimeSet
    {
        bool Contains(DateTime time);
    }
    /// <summary>
    /// 循环时间集
    /// </summary>
    public class CycleTimeSet : ITimeSet
    {
        public int Year = 2013;
        public int Month = 1;
        public int Day = 1;
        public int Hour = 0;
        public int Minute = 0;
        public int Second = 0;

        public bool Contains(DateTime time) 
        {
            if (Year != -1 && time.Year != Year)
                return false;
            if (Month != -1 && time.Month != Month)
                return false;
            if (Day != -1 && time.Day != Day)
                return false;
            if (Hour != -1 && time.Hour != Hour)
                return false;
            if (Minute != -1 && time.Minute != Minute)
                return false;
            if (Second != -1 && time.Second != Second)
                return false;
            return true;
        }

        public override string ToString()
        {
            return "{0}年{1}月{2}日{3}时{4}分{5}秒";
        }
    }
}
